########################################################################
# common.sh - common shell functions library
# Copyright (C) 2013 Mohamad Afifi Omar <afifio@gmail.com>
#
# These comments cannot be remove or edited and should remain intact
# because this file comments is an extension to the GPL3 license.
# This software file is covered by GPL version 3 license.
# Please see the LICENSE and gpl3.txt or gpl3.htm in doc directory
# for the license agreement, if its not exist, write to the 
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
# Modification below this line is allowed
########################################################################
# Extra credits to:
#
########################################################################
# Notes:
# To see whats available
# 		Uncomment 'export -f' in last line 
#		chmod +x common.sh
#		./common.sh
#		chmod -x common.sh
# check also enviroment()
########################################################################
export COMMONSHVERSION=20131029
#20130201
# Make program enviroment generic to this common libs
#export PROGNAME=$PROGNAME
#export PROGVER=$PROGVER
#export PROGDIR=$PROGDIR
#export PROGDOC=$PROGDOC
#export PROGLIB=$PROGLIB
#export PROGDLG=$PROGDLG
#export PROGPIX=$PROGPIX
#export PROGTMP=$PROGTMP
#export PROGAUTHOR=$PROGAUTHOR
#export PROGEMAIL=$PROGEMAIL

########################################################################
# AAA - Function template, copy, paste and replace fxfxfx
# Usage: AAA param1 param2 param3
########################################################################
AAA()
{
#	AAA param1 param2 param3  			# call in script
#   '"$(AAA param1 param2 param3)"' 	# call in xml
	echo 'AAA'
};export -f AAA

function gInitVar()
{
local redefined=$1$2
if [ -z "${!redefined}" ]
then
	echo "$3"
else
	echo "${!redefined}"
fi
};export -f gInitVar
function toArray ()
	{
	local index=0
	for arg in "$@"
	do
		echo -n " '$arg' "
		let "index+=1"
	done
	};export -f toArray
function gButton()
{
#gAssignVariable registryvarname registryvarsubname defaultvalue
local label=`gInitVar "$1" "label" 'Default Button'`
local variable=`gInitVar "$1" "variable" 'v'$RANDOM`
local icon=`gInitVar "$1" "icon" 'gtk-index'`
local sensitive=`gInitVar "$1" "sensitive" 'true'`
local signalclicked=`gInitVar "$1" "signalclicked" 'gActionNothing'`
echo '
<button>
	<label>'$label'</label>
	<variable>'$variable'</variable>
	<input file stock="'$icon'"></input>
	<sensitive>'$sensitive'</sensitive>
	<action signal="clicked">'$signalclicked'</action>
</button>'
};export -f gButton
function gButtonStock()
{
local stockicon=`gInitVar "$1" "icon" 'ok'`
local iconposition=`gInitVar "$1" "iconposition" '3'`
local signalclicked=`gInitVar "$1" "signalclicked" 'gActionNothing'`
echo '
	<button image-position="'$iconposition'" use-stock="true" label="gtk-'$stockicon'">'
	gVariable $1
	gSensitive $1
echo '<action signal="clicked">gSensitive "'$1'"</action>
</button>'
};export -f gButtonStock

##########################################################
#
# WIDGET DIRECTIVES
#
##########################################################
function gClearButton()
{
echo '<button relief="2"  tooltip="<i>Click to clear</i>">
	<input file icon="'$GMICON_ENTRY_CLEAR'"></input>
	<action>removeselected:'$1'</action>
	<action>save:'$1'</action>
	<action>refresh:'$1'</action>
</button>'
};export -f gClearButton
function gDefault()
{
	local redefined=D_$1_D
if [ -n "${!redefined}" ]
then
	echo '<default>'${!redefined}'</default>'
fi
};export -f gDefault
function gActionNothing()
{
	echo ""
};export -f gActionNothing
function gActionActivity()
{
local redefined=SD_$1_SD
if [ -n "${!redefined}" ]
then
	echo '<action>'${!redefined}'</action>'
fi
};export -f gActionActivity

function gActionFunction()
{
local stype=SFN_$1_SFN
local sactivity=SFP_$1_SFP
local ifssaved=$IFS; IFS="^";
eval actionsignal=(`toArray ${!stype}`)
eval actionactivity=(`toArray ${!sactivity}`)
IFS=$ifsaved
local element_count=${#actionsignal[@]}
local index=0
while [ "$index" -lt "$element_count" ]
	do    # List all the elements in the array.
		echo '<action type="'${actionsignal[$index]}'">'${actionactivity[$index]}'</action>'
		let "index = $index + 1"
	done
};export -f gActionFunction
function gActionSignal()
{
local stype=STN_$1_STN
local sactivity=STA_$1_STA
local ifssaved=$IFS; IFS="^";
eval actionsignal=(`toArray ${!stype}`)
eval actionactivity=(`toArray ${!sactivity}`)
IFS=$ifsaved
local element_count=${#actionsignal[@]}
local index=0
while [ "$index" -lt "$element_count" ]
	do    # List all the elements in the array.
		echo '<action signal="'${actionsignal[$index]}'">'${actionactivity[$index]}'</action>'
		let "index = $index + 1"
	done
};export -f gActionSignal

function gItem()
{
local items=ITM_$1_ITM
local ifssaved=$IFS; IFS="^";
eval itemlist=(`toArray ${!items}`)
IFS=$ifsaved
local element_count=${#itemlist[@]}
local index=0
while [ "$index" -lt "$element_count" ]
	do    # List all the elements in the array.
		echo '<item>'${itemlist[$index]}'</item>'
		let "index = $index + 1"
	done
};export -f gItem
function gInputCommand()
{
local redefined=CMD_$1_CMD
if [ -n "${!redefined}" ]
then
	echo '<input>'${!redefined}'</input>'
fi
};export -f gInputCommand
function gInputImg()
{
local redefined=IMG_$1_IMG
if [ -n "${!redefined}" ]
then
	echo '<input file>'${!redefined}'</input>'
fi
};export -f gInputImg
function gInputIcon ()
{
# /usr/share/pixmaps
# /usr/local/lib/X11/mini-icons
local icon=ICO_$1_ICO
case "$icon" in
	*gtk-*) echo '<input file stock="'$icon'"></input>'; 			return 1;;
	*) 		echo '<input file icon="'$PROGPIX'/'$icon'"></input>'; 	return 1;;
esac
};export -f gInputIcon


function gOutputFile()
{
local redefined=FO_$1_FO
if [ -n "${!redefined}" ]
then
	echo '<output file>'${!redefined}'</output>'
fi
};export -f gOutputFile

function gHeight()
{
local redefined=H_$1_H
if [ -n "${!redefined}" ]
then
	echo '<width>'${!redefined}'</width>'
fi
};export -f gHeight
function gWidth()
{
local redefined=$1label
if [ -n "${!redefined}" ]
then
	echo '<width>'${!redefined}'</width>'
fi
};export -f gWidth
function gLabel()
{
local redefined=L_$1_L
if [ -n "${!redefined}" ]
then
	echo '<label>'${!redefined}'</label>'
fi
};export -f gLabel

function gSensitive()
{
local redefined=$1sensitive
if [ -n "${!redefined}" ]
then
	echo '<sensitive>'${!redefined}'</sensitive>'
fi
};export -f gSensitive
function gVariable()
{
local redefined=V_$1_V
if [ -n "${!redefined}" ]
then
	echo '<variable>'${!redefined}'</variable>'
else
	if [ -n "$2" ]
	then
		echo '<variable>'$2'</variable>'
	else
		echo '<variable>v'$RANDOM'</variable>'
	fi
fi
};export -f gVariable
##########################################################
#
# WIDGET
#
##########################################################

function gCheckBox()
{
echo '
	<vbox>'
		gText $1
	echo '<checkbox space-fill="true" space-expand="true">'
		gLabel $1
		gVariable $1
	echo '</checkbox>
	</vbox>'
};export -f gCheckBox
function gColorButton()
{
local color=`gInitVar "$1" "color" "#88CC44"`
local alpha=`gInitVar "$1" "alpha" "65535"`
echo " $color | $alpha " > "$PROGTMP"/"$variable"
		gText $1
	echo '<colorbutton use-alpha="true">'
	echo '
		<input file>'$PROGTMP'/'$variable'</input>
		<output file>'$PROGTMP'/'$variable'</output>'
		gVariable $1
	echo '</colorbutton>'
};export -f gColorButton
function gComboboxEntry()
{
local variable=`gInitVar "$1" "variable" 'v'$RANDOM`
local icon=`gInitVar "$1" "icon" 'gtk-edit'`
echo '<vbox>'
gText $1
echo '<hbox>
					<button relief="1" tooltip="<i>Click to clear</i>">						
					'"$(isstock gtk-open)"'
						<action>removeselected:'$variable'</action>
						<action>save:'$variable'</action>
						<action>refresh:'$variable'</action>
						<action type="fileselect">'$variable'</action>
			<action>echo "'"$3"' '$variable'"</action>
					</button>
					<comboboxentry has-focus="true">'
gVariable $1 $variable
gDefault $1
echo '	<input file>'$PROGTMP'/'$variable'</input>
						<output file>'$PROGTMP'/'$variable'</output>
						<action signal="activate">save:'$variable'</action>
						<action signal="activate">refresh:'$variable'</action>
					</comboboxentry>'
					gClearButton $variable
echo '
</hbox></vbox>'
};export -f gComboboxEntry
function gEditbox()
{
	#   '"$(MouldEdit target file)"'
local variable=`gInitVar "$1" "variable" 'v'$RANDOM`
local file=`gInitVar "$1" "file" "textpad.txt"`
if [ ! -e $file ]
	then
		echo "#Filename: $2" > $file
fi
	echo '<vbox>'
	gText $1 $variable
	echo '<hbox space-fill="true" space-expand="true">
	'"$(ToolBarEditor $variable)"'
		<edit space-fill="true" space-expand="true" left-margin="5">
			<variable>'$variable'</variable>
			<input file>'$PROGTMP/"$file"'</input>
			<output file>'$PROGTMP/"$file"'</output>
		</edit>
	</hbox></vbox>'
};export -f gEditbox
function gEntrybox()
{
local variable=`gInitVar "$1" "variable" 'v'$RANDOM`
local icon=`gInitVar "$1" "icon" "gtk-edit"`
	echo '<vbox>'
	gText $1
	echo '<entry primary-icon-name="'$icon'" secondary-icon-name="gmouldGMICON_ENTRY_CLEAR" width-request="250"
		secondary-icon-activatable="true" secondary-icon-tooltip-markup="<i>Click to clear</i>"
		space-fill="true" space-expand="false">'
	gDefault $1
	gVariable $1 $variable
	echo '<action signal="secondary-icon-press">clear:'$variable'</action>
		</entry>
	</vbox>'
};export -f gEntrybox
function gHScale()
{
local variable=`gInitVar "$1" "variable" 'v'$RANDOM`
local widthrequest=`gInitVar "$1" "width-request" "420"`
local heightrequest=`gInitVar "$1" "height-request" "80"`
	echo '
<hscale width-request="'$widthrequest'" height-request="'$heightrequest'" range-value="4">
			<variable>'$variable'</variable>
			<action>gActionNothing</action>
		</hscale>'
};export -f gHScale
function gVScale()
{
local variable=`gInitVar "$1" "variable" 'v'$RANDOM`
local widthrequest=`gInitVar "$1" "width-request" "420"`
local heightrequest=`gInitVar "$1" "height-request" "80"`
	echo '
<vscale width-request="'$widthrequest'" height-request="'$heightrequest'" range-value="4">
			<variable>'$variable'</variable>
			<action>gActionNothing</action>
		</vscale>'
};export -f gVScale
function gPixmap()
{
local variable=`gInitVar "$1" "variable" 'v'$RANDOM`
local size=`gInitVar "$1" "size" "1"`
local stock=`gInitVar "$1" "stock" "gtk-index"`
	echo '
      <pixmap icon_size="'$size'">
        <input file stock="'$stock'"></input>
      </pixmap>'
};export -f gPixmap
function gProgressBar()
{
echo '<vbox>'
gText $1
echo '<progressbar>'
	gLabel $1
	gInputCommand $1
echo '</progressbar></vbox>'
};export -f gProgressBar
function gSpinButton()
{
	local variable=`gInitVar "$1" "variable" 'v'$RANDOM`
echo '<hbox>'
gText $1
echo '<spinbutton>'
	gVariable $1 $variable
	gSensitive $1
	gDefault $1
	gInputCommand $1
	gInputImg $1
	gOutputFile $1
echo '</spinbutton></hbox>'
};export -f gSpinButton
function gStatusBar()
{
echo '<statusbar>'
gLabel $1
gDefault $1
gVariable $1
gInputCommand $1
gInputImg $1
gOutputFile $1
gSensitive $1
echo '</statusbar>'
};export -f gStatusBar
function gText()
{
local redefined=$1text
if [ -n "${!redefined}" ]
then
	echo '<text xalign="0" space-fill="true" space-expand="true"><label>'${!redefined}'</label></text>'
fi
if [ -n "$2" ]
then
	echo '<text xalign="0" space-fill="true" space-expand="true"><label>'$2'</label></text>'
fi
};export -f gText
function gTimer()
{
echo '<timer>'
	gVariable $1
	gSensitive $1
	gAction $1
echo '<action signal="type">activity</action>
	<action type="function">parameter</action>'
echo '</timer>'
};export -f gTimer
########################################################################
# XMLExecute - Execute Gtkdialog
# Usage: XMLExecute param1 param2 param3
########################################################################
XMLExecute()
{
#	XMLRun param1 param2 param3  			# call in script
#   '"$(XMLRun param1 param2 param3)"' 		# call in xml

if [ "`which gtkdialog4`" ]; then
 GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
 GTKDIALOG=gtkdialog3
else
 GTKDIALOG=gtkdialog
fi
echo $GTKDIALOG $PROGLIB/$* 
};export -f XMLExecute
########################################################################
# XMLRun - Execute Gtkdialog
# Usage: XMLRun param1 param2 param3
########################################################################
XMLRun()
{
#	XMLRun param1 param2 param3  			# call in script
#   '"$(XMLRun param1 param2 param3)"' 		# call in xml

if [ "`which gtkdialog4`" ]; then
 GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
 GTKDIALOG=gtkdialog3
else
 GTKDIALOG=gtkdialog
fi

$GTKDIALOG --center --program=$* &
};export -f XMLRun
function XMLRunDialog()
{
#	XMLRun param1 param2 param3  			# call in script
#   '"$(XMLRun param1 param2 param3)"' 		# call in xml

if [ "`which gtkdialog4`" ]; then
 GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
 GTKDIALOG=gtkdialog3
else
 GTKDIALOG=gtkdialog
fi
local arg=$2
echo DialogArgName="${arg}" > $PROGTMP/DialogArg
echo DialogArgValue="${!arg}" >> $PROGTMP/DialogArg
source /root/projects/gmould/dlg/$1.dlg
#source $PROGDLG/$1.dlg
#TODO hard path modified for easier gmould development
# uncomment it for real development
$GTKDIALOG --center --program=DLG_$1 &
};export -f XMLRunDialog
########################################################################
# XMLCustom - Function template, copy, paste and replace fxfxfx
# Usage: XMLCustom param1 param2 param3
########################################################################
XMLCustom()
{
#	XMLCustom param1 param2 param3  			# call in script
#   '"$(XMLCustom param1 param2 param3)"' 		# call in xml
export XMLCUSTOM='<window title=" '$PROGNAME' EditorXML " icon-name="gtk-edit">
<vbox>
	<vbox space-fill="false" space-expand="false">
		'"$(MouldH4 gtk-edit "Custom XML" "Create custom XML.")"'
	</vbox>
	<hbox>
		<text width-request="150"><label>Tag:</label></text>
		<entry secondary-icon-name="'$GMICON_ENTRY_CLEARICON'" width-request="200" secondary-icon-activatable="true">
			<default>button</default>
			<variable>XMLTAG</variable>
			<action signal="secondary-icon-press">clear:XMLTAG</action>
		</entry>
	</hbox>
	<hbox>
		<text width-request="150"><label>Attribute:</label></text>
		<entry secondary-icon-name="'$GMICON_ENTRY_CLEARICON'" width-request="200" secondary-icon-activatable="true">
			<default>space-expand</default>
			<variable>XMLATT</variable>
			<action signal="secondary-icon-press">clear:XMLATT</action>
		</entry>
	</hbox>
	<hbox>
		<text width-request="150"><label>Attribute Value:</label></text>
		<entry secondary-icon-name="'$GMICON_ENTRY_CLEARICON'" width-request="200" secondary-icon-activatable="true">
			<default>true</default>
			<variable>XMLATTVALUE</variable>
			<action signal="secondary-icon-press">clear:XMLATTVALUE</action>
		</entry>
	</hbox>
	<hbox>
		<text width-request="150"><label>XML Content:</label></text>
		<entry secondary-icon-name="'$GMICON_ENTRY_CLEARICON'" width-request="200" secondary-icon-activatable="true">
			<default>true</default>
			<variable>XMLCONTENT</variable>
			<action signal="secondary-icon-press">clear:XMLCONTENT</action>
		</entry>
	</hbox>
	<button ok><action>createnewproject</action></button>
	<button cancel></button>
	</vbox>
</window>'
	XMLRun XMLCUSTOM
};export -f XMLCustom







########################################################################
# CustomMsgBox - Show a custom icon message box
# Usage: CustomMsgBox gtk-[icon] title text/file
########################################################################
CustomMsgBox()
{ 
#	CustomMsgBox gtk-[icon] title text/file 			# call in script
#   '"$(CustomMsgBox gtk-[icon] title text/file)"' 		# call in xml
	local icon=$1
	local title=$2
	shift
	shift
	MsgBox $icon Navy $title $@
};export -f CustomMsgBox
########################################################################
# Common Box - Show an info/warning/etc message box
# Usage: QuestionBox text/file
########################################################################
BoxQuestion(){ $PROGLIB/common.dlg MsgBox $PROGNAME question &
};export -f BoxQuestion
BoxAuthentication(){ $PROGLIB/common.dlg MsgBox $PROGNAME authentication &
};export -f BoxAuthentication
BoxWarning(){ $PROGLIB/common.dlg MsgBox $PROGNAME warning &
};export -f BoxWarning
BoxInfo(){ $PROGLIB/common.dlg MsgBox $PROGNAME info &
};export -f BoxInfo
BoxError(){ $PROGLIB/common.dlg MsgBox $PROGNAME error &
};export -f BoxError

########################################################################
# MsgBox - Call common dialog (external gtkdialog)
# Usage: MsgBox dialogtype 
########################################################################
MsgBox()
{
#	MsgBox dialogtype 			# call in script
#   '"$(MsgBox dialogtype)"' 		# call in xml
$PROGLIB/common.dlg About $PROGNAME &
};export -f MsgBox

########################################################################
# DialogInput - Simple script Editor
# Usage: DialogInput content
########################################################################
DialogInput()
{
#	DialogInput content  			# call in script
#   '"$(DialogInput content)"' 		# call in xml
Log INFO "Dialog input starting..."
content=$1
export COMMONDIALOG='
<window title=" '$PROGNAME' " icon-name="gtk-edit">
	<vbox>
	<vbox space-fill="false" space-expand="false">
		'"$(gmH3 gtk-edit "Information Required" "Please provide the details.")"'
	</vbox>
	'"$($content)"'
	<vbox space-expand="false" space-fill="false">
		<button ok><action type="exit">COMMONDIALOG</action></button>
	</vbox>
	</vbox>
</window>
'
XMLRun COMMONDIALOG
};export -f DialogInput
	
########################################################################
# Tester - Dialog to show available widget
# Usage: Tester
########################################################################
TesterWidget()
{
#   '"$(TesterWidget)"'
DialogInput MouldWidgetTest

};export -f TesterWidget
TesterDialog()
{
#   '"$(WidgetTester)"'
MouldDialog MouldHeadersTest "Headers" "Headers test."
};export -f TesterDialog

function MouldEdit()
{
	#   '"$(MouldEdit target file)"'
	if [ -z "$2" ]
	then
		file=$PROGTMP/textpad.txt
	else
		if [  ! -e $2 ]
		then
			echo "#Filename: $2" > $2
		fi
		file=$2
	fi
	echo '
	<hbox space-fill="true" space-expand="true">
	'"$(ToolBarEditor $1)"'
		<vbox>
			<text xalign="0" use-markup="true" wrap="false">
				<label>"File:<span color='"'DarkGreen'"'>'$file'</span>"</label>
			</text>
			<edit space-fill="true" space-expand="true" left-margin="10" file-monitor="true" auto-refresh="true">
				<variable export="false">'$1'</variable>
				<input file>'"$file"'</input>
				<output file>'"$file"'</output>
			</edit>
		</vbox>
	</hbox>'
};export -f MouldEdit
########################################################################
# EditorText - Simple script Editor
# Usage: EditorText 
########################################################################
EditorText()
{
#	EditorText  			# call in script
#   '"$(EditorText)"' 		# call in xml
Log INFO "Text Editor starting..."
export COMMONDIALOG='
<window title=" '$PROGNAME' Text Editor " icon-name="gtk-edit">
	<vbox>
	<vbox space-fill="false" space-expand="false">
		'"$(gmH3 gmouldtext.svg "Text Editor" "Text editor.")"'
	</vbox>
		<hbox space-fill="true" space-expand="true">
			'"$(MouldEdit TextEditbox $TEXTPAD)"'
			<hbox space-fill="true" space-expand="false">
				'"$(MouldEdit SnippetEditbox $NOTEPAD)"'
			</hbox>
		</hbox>

	<vbox space-expand="false" space-fill="false">
		<button ok><action type="exit">COMMONDIALOG</action></button>
	</vbox>
	</vbox>
</window>
'
XMLRun COMMONDIALOG
};export -f EditorText

########################################################################
# EditorToolBar - Toolbar Editor
# Usage: EditorToolBar 
########################################################################
function EditorToolBar()
{
#	EditorToolBar  			# call in script
#   '"$(EditorToolBar)"' 		# call in xml
Log INFO "Toolbar Editor starting..."
export COMMONDIALOG='
<window title=" '$PROGNAME' Toolbar Editor " icon-name="gtk-edit" resizable="false">
	<vbox>
	<vbox space-fill="false" space-expand="false">
		'"$(gmH3 gmouldscript.svg "Toolbar Editor" "Toolbar editor.")"'
	</vbox>
	<hbox>
		<vbox space-fill="true" space-expand="false">
		'"$(MouldEntry "vToolBarFile" "Toolbar File" "varToolBarFileName" $GMICON_FILE)"'
		'"$(MouldEntry "vToolBarName" "Toolbar Name" "ToolBarName" $GMICON_EDIT)"'
		'"$(MouldCheckBox  "vToolBarShowTitle" "Show Title" "Yes" $GMICON_APPLY)"'
		'"$(MouldComboboxEntry "vToolBarSize" "Button Size" "22" $GMICON_SAVE)"'
		'"$(MouldComboboxEntry "vToolBarStyle" "Button Style" "3" $GMICON_STYLE)"'
		'"$(MouldComboboxEntry "vToolBarTextPosition" "Text Position" "3" $GMICON_POSTITION)"'
		</vbox>
		<tree></tree>
		<vbox>
		'"$(MouldEntry "vToolButtonLabel" "Label" "ToolButtonX" )"'
		'"$(MouldEntry "vToolButtonTooltip" "Tooltip" "TooltipX" )"'
		'"$(MouldComboboxEntry "vToolButtonIcon" "Icon" "gtk-apply" $GMICON_ICON)"'
		'"$(MouldComboboxEntry "vToolButtonAction" "Action" "Nothing" $GMICON_ACTION)"'
		</vbox>
	</hbox>
	<vbox space-expand="false" space-fill="false">
		<button ok><action type="exit">COMMONDIALOG</action></button>
	</vbox>
	</vbox>
</window>
'
XMLRun COMMONDIALOG
};export -f EditorToolBar

########################################################################
# MouldSectionPage - Placeholder widget for dev purpose
# Usage: MouldSectionPage icon title text
########################################################################
MouldSectionPage()
{
if [ ! -z "$1" ]
then
	varname=$1
else
	varname="Sectional"
fi
Log INFO "MouldSectionPage starting with arg..."$varname
source $PROGLIB/$varname.dlg
numberofsections=${#DLGSECTIONPAGE[@]}
data=0
while [ "$data" -lt "$numberofsections" ]
do
	${DLGSECTIONPAGE[$data]}
	let "data += 1"
done
		# header below is for default if all section fail to display
		MouldH4  gtk-dialog-info Information "This section is under improvement and will be available in future versions."
};export -f MouldSectionPage
########################################################################
# MouldSectionBar - Sections toolbar
# Usage: MouldSectionBar varname filename
########################################################################
MouldSectionBar()
{
#	MouldSectionBar varname filename			# call in script
#   '"$(MouldSectionBar varname filename)"' 		# call in xml

if [ ! -z "$1" ]
then
	varname=$1
else
	varname="Sectional"
fi
local data=0
source $PROGLIB/$varname.dlg
icons=${#DLGSECTIONICON[@]}

while [ "$data" -lt "$icons" ]
do
	echo '
		<button relief="2" image-position="2" tooltip-text="'${DLGSECTIONTEXT[$data]}'">
			<height>'$DLGSECTIONICONSIZE'</height>
			<width>'$DLGSECTIONICONSIZE'</width>
			<input file icon="'${DLGSECTIONICON[$data]}'"></input>
			<action>echo '"$data"' > '"$PROGTMP"'/'$varname'</action>
			<action>refresh:'$varname'Notebook</action>
			<label>'${DLGSECTIONLABEL[$data]}'</label>
		</button>
'
	let "data += 1"
done
};export -f MouldSectionBar
########################################################################
# DialogSectional - Call common settings/Preferences dialog (external gtkdialog))
# Usage: DialogSectional varname
########################################################################
DialogSectional()
{
#	DialogSectional varname 			# call in script
#   '"$(DialogSectional varname)"' 		# call in xml
if [ ! -z "$1" ]
then
	DLGVARNAME=$1
else
	DLGVARNAME="Sectional"
fi
source $PROGLIB/$DLGVARNAME.dlg
echo 0 > $PROGTMP/$DLGVARNAME
export COMMONDIALOG='
<window title=" '$DLGTITLE' " image-name="/usr/share/icons/'$DLGICON'.svg" resizable="true" window_position="1">
<vbox>
	<vbox space-expand="false" space-fill="false">
		'"$(gmH3 $DLGICON "$DLGTITLE" "$DLGDESC")"'
	</vbox>
	<vbox space-expand="true" space-fill="true">
		<frame>
			<hbox space-expand="true" space-fill="true">
				<hbox space-expand="false" space-fill="false">
					<vbox space-expand="false" space-fill="false" scrollable="false">
						'"$(MouldSectionBar $DLGVARNAME)"'
					</vbox>
				</hbox>
				<hbox space-expand="true" space-fill="true">
					<vbox space-expand="true" space-fill="true">
						<notebook labels="'$DLGSECTIONS'" show-tabs="false" show-border="false" space-expand="true" space-fill="true">
							'"$(MouldSectionPage $DLGVARNAME)"'
							<input file>'"$PROGTMP"'/'$DLGVARNAME'</input>
							<variable>'$DLGVARNAME'Notebook</variable>
						</notebook>
					</vbox>
				</hbox>
			</hbox>
		</frame>
	</vbox>
	<hbox space-expand="false" space-fill="false">
		<button ok><action>'$2'</action></button>
		<button cancel><action type="exit">COMMONDIALOG</action></button>
	</hbox>
</vbox>
</window>
'
XMLRun COMMONDIALOG
# $PROGLIB/common.dlg Preferences $PROGNAME &
};export -f DialogSectional
########################################################################
# DialogStockIcon - Dialog to select the stock icon
# Usage: DialogStockIcon
########################################################################
DialogStockIcon()
{
#	DialogStockIcon  			# call in script
#   '"$(DialogStockIcon)"' 		# call in xml
export COMMONDIALOG='
<window title=" Select an icon " icon-name="gtk-select-color">
	<vbox>
		<vbox space-fill="true" space-expand="false">
		'"$(gmH3 gmouldstock.svg "Stock Icon Selector" "Select an icon.")"'
		</vbox>
		<hbox space-fill="true" space-expand="true">
			<tree rules-hint="true" exported-column="0">
				<label>Stock Icons</label>
				<variable>'$1'</variable>
				<input file stock-column="0">'$PROGLIB'/icon_names.txt</input>
				<output file>'$PROGTMP'/'$1'</output>
				<action signal="cursor-changed">echo $'$1' > '$PROGTMP'/'$1'</action>
			</tree>
		</hbox>
	<vbox space-expand="false" space-fill="false">
		<button ok><action type="exit">COMMONDIALOG</action></button>
	</vbox>
	</vbox>
</window>
'
XMLRun COMMONDIALOG
};export -f DialogStockIcon

########################################################################
# MouldDialog - Simple script Editor
# Usage: MouldDialog title content
########################################################################
MouldDialog()
{
#	DialogInput content  			# call in script
#   '"$(DialogInput content)"' 		# call in xml
Log INFO "Dialog input starting..."
content=$1
title=$2
desc=$3
export COMMONDIALOG='
<window title=" '$PROGNAME' '$title' " icon-name="gtk-edit">
	<vbox>
	<vbox space-fill="false" space-expand="false">
		'"$(gmH3 gtk-edit "$title" "$desc")"'
	</vbox>
	'"$($content)"'
	<vbox space-expand="false" space-fill="false">
		<button ok><action type="exit">COMMONDIALOG</action></button>
	</vbox>
	</vbox>
</window>
'
XMLRun COMMONDIALOG
};export -f MouldDialog
########################################################################
# CBox - Show a message box
# type: authentication, warning, info, error or question
# color: title color
# title: title text
# file: text source from a file - filename or ""
# 		file is in program tmp dir
# Usage: CBox type color file text
########################################################################
CBox()
{
#   '"$(CBox info blue Information "error")"'
local icon=$1
echo '
<variable>BOX</variable>
<vbox>
	<frame>
		<hbox>
			<pixmap icon_size="6">
				<input file stock="gtk-'$1'"></input>
			</pixmap>
			<vbox space-expand="true" space-fill="true">
			<text use-markup="true" xalign="0">
			<label>"<span color='"'$2'"' font-family='"'Sans'"' weight='"'bold'"' size='"'x-large'"'><i>'"$3"'</i></span>"</label>
			</text>
			<text use-markup="true" space-expand="true" space-fill="true" xalign="0">'
			if [ -f "$PROGTMP/$4" ]
			then
					echo '<input file>'"$PROGTMP"'/'"$4"'</input>'
			else
					echo '<label>No error "'$PROGTMP'/'$4'"</label>'
			fi
			echo '</text>
			</vbox>
		</hbox>
	</frame>
	<button image-position="0" use-stock="true" label="gtk-'$icon'">
		<action type="closewindow">BOX</action>
	</button>
</vbox>
'
};export -f CBox

########################################################################
# MouldH6 - Section header
# Usage: MouldH6 icon title text
########################################################################
MouldH6()
{
#   '"$(MouldH6 icon title text)"'
echo '
<vbox spacing="0" space-expand="true" space-fill="true" >
<frame>
<hbox spacing="0">
	<pixmap space-expand="false" space-fill="true">
		<width>16</width>
		'
		isstock $1
echo '
	</pixmap>
<text use-markup="true" space-expand="true" space-fill="true" xalign="0">
<label>"<span color='"'MidnightBlue'"' font-family='"'Sans'"'><u><i>'$2'</i></u></span>"
</label>
</text>
</hbox>
<text use-markup="true" space-expand="true" space-fill="true" xalign="0" wrap="false">
<label>"<span color='"'MidnightBlue'"'><small><i>'$3'</i></small></span>"
</label></text>
</frame>
</vbox>'
};export -f MouldH6
########################################################################
# MouldH5 - Section header
# Usage: MouldH5 icon title text
########################################################################
MouldH5()
{
#   '"$(MouldH5 icon title text)"'
echo '
<vbox space-fill="true" space-expand="true">
<frame>
<hbox spacing="0">
	<pixmap space-expand="false" space-fill="true">
		<width>22</width>
		'
		isstock $1
echo '
	</pixmap>
<text use-markup="true" space-expand="true" space-fill="true" xalign="0">
<label>"<span color='"'MidnightBlue'"' font-family='"'Sans'"' weight='"'bold'"'><u><i>'$2'</i></u></span>"
</label>
</text>
<text use-markup="true" space-expand="true" space-fill="true" xalign="0" wrap="false">
<label>"<span><i>'$3'</i></span>"</label></text>
</hbox>
</frame>
</vbox>'
};export -f MouldH5
########################################################################
# MouldH4 - Section header
# Usage: MouldH4 icon title text
########################################################################
MouldH3()
{
#   '"$(MouldH4 icon title text)"'
echo '
<vbox space-fill="true" space-expand="true">
<hbox>
	<pixmap space-fill="true" space-expand="false">
		<width>32</width>
		'
		isstock $1
echo '
	</pixmap>
<text use-markup="true" space-expand="true" space-fill="true" xalign="0">
<label>"<span color='"'LightSlateBlue'"' font-family='"'Sans'"'><u><i><big>'$2'</big></i></u></span>"
</label>
</text>
<text use-markup="true" space-expand="true" space-fill="true" xalign="1">
<label>"<span><i>'$3'</i></span>"
</label></text>
	</hbox><hseparator width-request="75"></hseparator>
</vbox>'
};export -f MouldH3
MouldH4()
{
#   '"$(MouldH4 icon title text)"'
echo '
<vbox space-fill="true" space-expand="true">
<hbox>
	<pixmap space-fill="true" space-expand="false">
		<width>24</width>
		'
		isstock $1
echo '
	</pixmap>
<text use-markup="true" space-expand="true" space-fill="true" xalign="0">
<label>"<span color='"'LightSlateBlue'"' font-family='"'Sans'"'><u><i><big>'$2'</big></i></u></span>"
</label>
</text>
<text use-markup="true" space-expand="true" space-fill="true" xalign="1">
<label>"<span><i>'$3'</i></span>"
</label></text>
	</hbox><hseparator width-request="75"></hseparator>
</vbox>'
};export -f MouldH4
########################################################################
# gmH3 - Section header
# Usage: gmH3 icon title text
########################################################################
gmH3()
{
#   '"$(gmH3 icon title text)"'
echo '
<vbox space-fill="true" space-expand="false">
<hbox>
	<pixmap space-fill="true" space-expand="false">
		<width>32</width>
		'
		isstock $1
echo '
	</pixmap>
<text use-markup="true" space-expand="true" space-fill="true" xalign="0">
<label>"<span color='"'SlateBlue'"' font-family='"'Sans'"' size='"'large'"'><big>'$2'</big></span>"
</label></text>
<text use-markup="true" space-expand="true" space-fill="true" xalign="1">
<label>"<span >'$3'</span>"
</label></text>
	</hbox><hseparator width-request="75"></hseparator>
</vbox>'
};export -f gmH3
########################################################################
# MouldH2 - Section header
# Usage: MouldH2 icon title text
########################################################################
MouldH2()
{
#   '"$(MouldH2 icon title text)"'
echo '
<vbox space-fill="true" space-expand="true">
<hbox space-fill="true" space-expand="true">
	<pixmap space-fill="true" space-expand="false">
		<width>48</width>
		'
		isstock $1
echo '
	</pixmap>
<text use-markup="true" space-expand="true" space-fill="true" xalign="1">
<label>"<span  color='"'CornflowerBlue'"' font-family='"'arial'"' size='"'x-large'"'><big>'$2'</big></span>"
</label>
</text>
	</hbox><hseparator width-request="75"></hseparator>
</vbox>'
};export -f MouldH2
########################################################################
# MouldH1 - Section header
# Usage: MouldH1 icon title text
########################################################################
MouldH1()
{
#   '"$(MouldH1 icon title text)"'
echo '
<vbox space-expand="true" space-fill="true">
<hbox>
	<pixmap space-expand="false" space-fill="true">
		<width>64</width>
		'
		isstock $1
echo '
	</pixmap>
<text use-markup="true" space-expand="true" space-fill="true" xalign="1">
<label>"<span  color='"'CornflowerBlue'"' font-family='"'Sans'"' size='"'xx-large'"' weight='"'bold'"' ><big>'$2'</big></span>"
</label>
</text>
	</hbox>
</vbox>'
};export -f MouldH1
########################################################################
# bootstrap - Load whatever we need
# Usage: bootstrap
########################################################################
bootstrap()
{
Log INFO "Bootstrapping "$PROGNAME
# source the program functions and dialog
source $PROGLIB/"$PROGNAME".lib.sh
source $PROGLIB/"$PROGNAME".sh
echo 0 > $PROGTMP/preference
Log INFO "Bootstrap completed"

};export -f bootstrap

########################################################################
# fxargument - List argument passed to function
# Usage: fxargument param1 param2 param3
########################################################################
fxargument()
{
#	fxargument param1 param2 param3
#   '"$(fxargument param1 param2 param3)"'
if $DEBUG = "true"
then
	ifs=$IFS; IFS=;
	echo "Listing args with \"\$@\":"
	for arg in "$@"
	do
		echo "Arg $index = $arg"
		let "index+=1"
	done
	IFS=$ifs
	echo "Here is what I receive: $*"
fi
};export -f fxargument
########################################################################
# environment - Export enviroment to /tmp/enviroment
# Usage: environment
########################################################################
environment()
{
#	environment  			# call in script
#   '"$(environment)"' 		# call in xml
env > /tmp/enviroment
echo "" >> /tmp/enviroment
echo "" >> /tmp/enviroment
echo "########################################################################" >> /tmp/enviroment
echo "# Exported enviroment" >> /tmp/enviroment
export >> /tmp/enviroment
echo "" >> /tmp/enviroment
echo "" >> /tmp/enviroment
echo "########################################################################" >> /tmp/enviroment
echo "# Exported functions" >> /tmp/enviroment
export -f >> /tmp/enviroment

};export -f environment


########################################################################
# ToolBar - Toolbar buttons
# Usage: ToolBar file
########################################################################
ToolBar()
{
#   '"$(ToolBar file)"'
local data=0
source $PROGLIB/$1
buttons=${#TBBUTTONICON[@]}
#let TBBUTTONSIZE=$TBBUTTONSIZE+4
echo '<hbox spacing="0">'
	if [ $TBSHOWTITLE = "1" ]
	then
	echo '<text><label>'$TBTITLE':</label></text>'
	fi
while [ "$data" -lt "$buttons" ]
do
	echo '<button relief="2" '
	#if [ $TBBUTTONSTYLE = "2" ]
	#then
	#	echo 'width-request="32" '
	#fi
	echo 'image-position="'$TBBUTTONTEXTPOSITION'" tooltip-text="'${TBBUTTONTOOLTIP[$data]}'" space-fill="false" space-expand="false">'
	case $TBBUTTONSTYLE in
		1)	echo '<label>'${TBBUTTONLABEL[$data]}'</label>'
			;;
		2)	isstock ${TBBUTTONICON[$data]}
			echo '<width>'$TBBUTTONSIZE'</width>'
			;;
		*)	echo '<label>'${TBBUTTONLABEL[$data]}'</label>'
			isstock ${TBBUTTONICON[$data]}
			echo '<height>'$TBBUTTONSIZE'</height>'
			;;
	esac
	
	echo ${TBBUTTONACTION[$data]}'</button>'
	let "data += 1"
done
echo '
	<hbox space-fill="true" space-expand="true">
		<button image-position="0" use-stock="true" label="gtk-help" space-fill="false" space-expand="false">
			<action>XMLRunDialog Help</action>
		</button>
	</hbox>
</hbox>
'
};export -f ToolBar
########################################################################
# ToolboxButton - Tool box button creator
# Usage: ToolboxButton param1 tooltip param3
########################################################################
ToolboxButton()
{
#	ToolboxButton param1 tooltip param3  			# call in script
#   '"$(ToolboxButton param1 tooltip param3)"' 		# call in xml<label>'$2'</label>

#<hbox homogeneous="false">
#<text space-fill="false" space-expand="true" >
#	<label>'$2'</label>
#</text>
#</hbox>
echo '
<button tooltip-text=" '$2' " space-fill="false" space-expand="false">
	<input file icon="'$1'"></input>
	<action signal="clicked">" '$3' "</action>
	<action signal="clicked">refresh:'$4'</action>
</button>
'
};export -f ToolboxButton
ToolboxButtonBlank()
{
#	ToolboxButton param1 tooltip param3  			# call in script
#   '"$(ToolboxButton param1 tooltip param3)"' 		# call in xml<label>'$2'</label>

#<hbox homogeneous="false">
#<text space-fill="false" space-expand="true" >
#	<label>'$2'</label>
#</text>
#</hbox>
echo '
<button tooltip-text=" Blank " space-fill="false" space-expand="false" >
	<sensitive>false</sensitive>
	<input file>$GMICON_CUSTOM</input>
</button>
'
};export -f ToolboxButtonBlank
########################################################################
# ToolBarEditor - Editbox toolbar buttons
# Usage: ToolBarEditor file
########################################################################
ToolBarEditorButton()
{
	#   '"$(ToolBarEditorButton target)"'
	echo '
	<button relief="2" space-fill="false" space-expand="false" tooltip-text="'$4'">
		'"$(isstock $1)"'
		<action type="'$2'">'$3'</action>
		<width>16</width>
	</button>'
};export -f ToolBarEditorButton

########################################################################
# ToolBarEditor - Toolbar buttons for the edit box
# Usage: ToolBarEditor file<input file stock="'$1'"></input>
########################################################################
ToolBarEditor()
{
#   '"$(ToolBarEditor target)"'
echo '
<vbox space-fill="false" space-expand="false" spacing="0">
	'$(ToolBarEditorButton $GMICON_EDIT_SAVE Save $1 "Save the file")'
	'$(ToolBarEditorButton $GMICON_EDIT_RELOAD Refresh $1 "Reload the file")'
	'$(ToolBarEditorButton $GMICON_EDIT_CUT Removeselected $1 "Cut the selected text")'
	'$(ToolBarEditorButton $GMICON_EDIT_CLEAR Clear $1 "Clear the editor")'
	'$(ToolBarEditorButton $GMICON_EDIT_DISABLE Disable $1 "Disable the editor")'
	'$(ToolBarEditorButton $GMICON_EDIT_ENABLE Enable $1 "Enable the editor")'
	'$(ToolBarEditorButton $GMICON_EDIT_HIDE Hide $1 "Hide the editor")'
	'$(ToolBarEditorButton $GMICON_EDIT_SHOW Show $1 "Show the editor")'
</vbox>
'
};export -f ToolBarEditor
########################################################################
# isstock - return 1 if "gtk-" is in the text, stock icons checking
# Usage: isstock text
########################################################################

function isstock ()
{
# if s2 points to a string of zero length, strstr echoes s1
# [ ${#2} -eq 0 ] && { return 0; }

case "$1" in
	*gtk-*) echo '<input file stock="'$1'"></input>'; 			return 1;;
	*gmould-*) echo '<input file icon="'$1'"></input>'; 			return 1;;
	*) 		echo '<input file>"'$PROGPIX'/'$1'"</input>'; 	return 1;;
esac
};export -f isstock

########################################################################
# TreeLoggerClear - clear the DialogTreeLogger by icon name
# Usage: TreeLoggerClear gtk-stockiconname
########################################################################
function TreeLoggerClear()
{
#   '"$(TreeLoggerClear gtk-stockiconname)"'
sed -e '/'$1'/d ' $PROGTMP/log > $PROGTMP/log1
rm -f $PROGTMP/log
mv -f $PROGTMP/log1 $PROGTMP/log
};export -f TreeLoggerClear

########################################################################
# Log - application logging, use DialogTreeLogger to view it. Default log file
# is in ~/.config/application/tmp/log and cleared everytime app restart
# Usage: Log type message or use gtk-stockicon for custom log
# type: INFO WARNING ERROR QUESTION AUTHENTICATION 
########################################################################
function Log()
{
#   '"$(Log INFO/WARNING/ERROR/AUTHENTICATION/gtk-edit)"'
callingfx=`caller 0`
case $1 in
"INFO") 			icon="gtk-dialog-info";;
"WARNING") 			icon="gtk-dialog-warning";;
"ERROR") 			icon="gtk-dialog-error";;
"AUTH") 			icon="gtk-dialog-authentication";;
*) 					icon=$1;;
esac
shift
echo $icon " | "`date +%X` "|"  $@"|" $callingfx >> $PROGTMP/log
};export -f Log

AssignLabel()
{
if [ -z "$1" ]
then
	if [ -z "$2" ]
	then
		echo "Label "$RANDOM
	else
		echo "$2"
	fi
else
	echo "$1"
fi
};export -f AssignLabel
AssignVariable()
{
if [ -z "$1" ]
then
	echo "var"$RANDOM
else
	echo "$1"
fi
};export -f AssignVariable
AssignDefault()
{
if [ -z "$1" ]
then
	if [ -z "$2" ]
	then
		echo "Undefined"
	else
		echo "$2"
	fi
else
	echo "$1"
fi
};export -f AssignDefault
AssignIcon()
{
if [ -z "$1" ]
then
	if [ -z "$2" ]
	then
		echo "gtk-apply"
	else
		echo "$2"
	fi
else
	echo "$1"
fi
};export -f AssignIcon
########################################################################
# MouldColorButton - Entry widget
# Usage: MouldSpinButton variable label default icon
########################################################################

########################################################################
# MouldRadioButton - Entry widget
# Usage: MouldRadioButton variable label default icon
########################################################################
MouldRadioButton()
{
#	MouldRadioButton variable label icon action  			# call in script
#   '"$(MouldRadioButton variable label icon action)"' 	# call in xml
variable=`AssignVariable "$1"`
label=`AssignLabel "$2" "Select one"`
labelradio=`AssignLabel "$3" "Select this"`
echo	'
	<radiobutton>
		<label>'$labelradio'</label>
		<variable>'$variable'</variable>
	</radiobutton>'
};export -f MouldRadioButton
########################################################################
# MouldTreeview - Entry widget
# Usage: MouldTreeview variable label default icon
########################################################################
MouldTreeview()
{
#	MouldTreeview variable label headers   			# call in script
#   '"$(MouldTreeview variable label headers )"' 	# call in xml
variable=`AssignVariable "$1"`
label=`AssignLabel "$2" "List"`
headers=`AssignLabel "$3" "First|Second|Third|Fourth"`
echo	'
	<hbox>
  <frame '$label'>
    <tree icon-name="'$TREEITEMICON'" rules-hint="true" enable-tree-lines="true" exported-column="0,1,2,3">
      <variable>'$variable'</variable>
      <label>'$headers'</label>
      <input file>'$PROGTMP'/'$variable'</input>
      <output file>'$PROGTMP'/'$variable'</output>
      <action>echo $'$variable' > fSelectedTree</action>
      <action>refresh:ENTRY</action>
    </tree>
      <entry>
        <variable>ENTRY</variable>
        <input file>fSelectedTree</input>
      </entry>
    <hbox homogeneous="true">
      <button tooltip-text="Insert a new node">
		<label>Insert</label>
        <input file icon="'$TREEEDITICON'"></input>
        <action>echo "$ENTRY" >> '$PROGTMP'/'$variable'</action>
        <action type="refresh">'$variable'</action>
      </button>
      <button tooltip-text="Prepare nodes to copy">
        <input file icon="'$TREEZIPICON'"></input>
        <label>Combine</label>
        <action>sed -e "s/|/^/g" '$PROGTMP'/'$variable' > '$PROGTMP'/'$variable'tmp; mv -f '$PROGTMP'/'$variable'tmp '$PROGTMP'/'$variable'</action>
        <action type="refresh">'$variable'</action>
      </button>
      <button tooltip-text="Copy the node">
        <input file icon="'$TREECOPYICON'"></input>
        <label>Copy</label>
        <action>echo "$'$variable'" >> '$PROGTMP'/'$variable'</action>
        <action type="refresh">'$variable'</action>
      </button>
      <button tooltip-text="Expand the nodes">
        <input file icon="'$TREEUNZIPICON'"></input>
        <label>Expand</label>
        <action>sed -e "s/\^/|/g" '$PROGTMP'/'$variable' > '$PROGTMP'/'$variable'tmp; mv -f '$PROGTMP'/'$variable'tmp '$PROGTMP'/'$variable'</action>
        <action type="refresh">'$variable'</action>
      </button>
      <button tooltip-text="Delete the node">
        <input file icon="'$GMICON_NODE_DELETE'"></input>
        <label>Delete</label>
        <action type="removeselected">'$variable'</action>
        <action type="save">'$variable'</action>
        <action>sed -e "s/"$TREEITEMICON"||//g" '$PROGTMP'/'$variable' > '$PROGTMP'/'$variable'tmp; mv -f '$PROGTMP'/'$variable'tmp '$PROGTMP'/'$variable'; echo " " >> '$PROGTMP'/'$variable'</action>
        <action type="refresh">'$variable'</action>
      </button>
    </hbox>   
  </frame>
	</hbox>'
};export -f MouldTreeview
########################################################################
# MouldCheckBox - Entry widget
# Usage: MouldCheckBox variable label default icon
########################################################################


########################################################################
# MouldStockButton - Entry widget
# Usage: MouldButton variable label default icon
########################################################################
MouldStockButton()
{
#	MouldStockButton variable icon  			# call in script
#   '"$(MouldStockButton variable icon)"' 	# call in xml
variable=`AssignVariable "$1" "StockButton"`
icon=`AssignIcon "$2" "gtk-about"`
echo '
	<button image-position="0" use-stock="true" label="'$icon'">
		<variable>'$variable'</variable>
		<action>""</action>
	</button>'
};export -f MouldStockButton
########################################################################
# MouldButton - Entry widget
# Usage: MouldButton variable label default icon
########################################################################
MouldButton()
{
#	MouldButton variable label icon action  			# call in script
#   '"$(MouldButton variable label icon action)"' 	# call in xml
variable=`AssignVariable "$1"`
label=`AssignLabel "$2" "Button"`
icon=`AssignIcon "$3"`

if [ ! -z "$4" ]
then
	action=$4
	else
	action="\"\""
fi
	echo '
	<hbox>
		<button>
				<variable>'$variable'</variable>
				<label>'$label'</label>
				<height>16</height>'
				isstock $icon
	echo '		<action>'$action'</action>'
echo '</button>
	</hbox>'
};export -f MouldButton

########################################################################
# MouldEntry - Entry widget
# Usage: MouldEntry variable label default icon
########################################################################



########################################################################
# MouldComboboxFileSelect - Entry widget
# Usage: MouldComboboxFileSelect variable label default icon
########################################################################
MouldComboboxFileSelect()
{
#	MouldComboboxFileSelect variable label default icon  			# call in script
#   '"$(MouldComboboxFileSelect variable label default icon)"' 	# call in xml
variable=`AssignVariable "$1"`
label=`AssignLabel "$2" "Type or select something"`
default=`AssignDefault "$3" "Type something here..."`
icon=`AssignIcon "$4" "gtk-jump-to"`
echo '
<hbox>
	<text xalign="0" space-fill="true" space-expand="true">
						<label>'$label'</label>
					</text>
					<pixmap icon_size="1">'
						isstock $icon
echo '				
					<width>16</width>
					</pixmap>
					<comboboxentry has-focus="true">
						<variable>'$variable'</variable>
						<default>'$default'</default>
						<input file>'$PROGTMP'/'$variable'</input>
						<output file>'$PROGTMP'/'$variable'</output>
						<action signal="activate">save:'$variable'</action>
						<action signal="activate">refresh:'$variable'</action>
					</comboboxentry>
					<button relief="2">
						<input file>"'$PROGPIX'/'$GMICON_ENTRY_CLEARICON'"</input>
						<action>removeselected:'$variable'</action>
						<action>save:'$variable'</action>
						<action>refresh:'$variable'</action>
					</button>
</hbox>'
};export -f MouldComboboxFileSelect
function tagAction()
{
echo '<action>'$1'</action>'
};export -f tagAction
function ActionSave()
{
echo '<action>save:'$1'</action>'
};export -f ActionSave
function ActionRefresh()
{
echo '<action>refresh:'$1'</action>'
};export -f ActionRefresh
function ActionRemoveSelected()
{
echo '<action>removeselected:'$1'</action>'
};export -f ActionRemoveSelected

function MouldWidgetTest()
{
MouldTreeview "vTreeViewTest" "List" 
MouldEntry "vFxName" "Function Name" "FxTemplate" "gtk-execute" 
MouldEntry "" "" "" ""
MouldStockButton
MouldStockButton "vStockButtonTest" "gtk-print"
MouldButton "vButtonTest" "SampleLabel" "gtk-print"
MouldButton "" "" "" ""
MouldComboboxEntry "vComboBoxEntryTest" "SampleLabel" "DefaultSample" "gtk-execute"

gCheckBox mycheckbox1
gCheckBox  mycheckbox2
MouldRadioButton "" "" "" ""
MouldRadioButton "vRadioBtnCheckTest" "SampleLabel" "true" "gtk-execute"
MouldColorButton "" "" "" ""
MouldColorButton "vColorBtnCheckTest" "SampleLabel" "#88CC44" "65535"
};export -f MouldWidgetTest
############################################################

function DCExplode()
{
local start=$1
local end=$2
shift; shift
for arg in "$@"
	do
		echo "$start$arg$end"
done
};export -f DCExplode
function DCImplode()
{
local start=$1
shift;
local result=""
for arg in "$@"
	do
			echo -n $start$arg
	done
};export -f DCImplode
function CSVexplode()
{
local start=$1
local end=$2
shift; shift
	for arg in "$@"
	do
		echo "$start$arg$end"
	done
};export -f CSVexplode
############################################################
function CSVfromfile()
{
CSVfrompipe < $1
};export -f CSVfromfile
############################################################
function CSVtofile()
{
# Usage:
# file=output.txt ;CSV='Hello^world^!'
# CSVtofile $file $CSV
before=""
after=""
local file=$1
shift
local tmpvar=$*
local count=$#
local ifssaved=$IFS; IFS="^";
CSVexplode "" "" $tmpvar > $file
IFS=$ifsaved
};export -f CSVtofile

############################################################

function CSVtoarray()
{
# Usage:
# CSV='Hello^world^!'
# CSVtoarray $CSV
local ifssaved=$IFS; IFS="^";
CSVexplode "'" "' " $1
IFS=$ifsaved
};export -f CSVtoarray
############################################################
function CSVfrompipe()
{
# Usage:
# file=list.txt
# CSVfrompipe < list.txt
local result=""
	while read line
		do
			if [ "$result" = "" ]
			then
				result=$line
			else
				result=$result^$line
			fi
		done
	echo $result
};export -f CSVfrompipe

############################################################
function CSVimplode()
{
# Usage:
# a=Hello; b=world; c=everyone
# CSVimplode $a $b and $c !
local result=""
for arg in "$@"
	do
		if [ "$result" = "" ]
		then
			result=$arg
		else
			result=$result^$arg
		fi
	done
		echo $result
};export -f CSVimplode
function gContainerHideShow()
{
local label=$1
if [ -n "$2" ]
then
	label=$2
fi
echo '<hbox space-fill="true" space-expand="false" spacing="0">
	<text space-expand="true" xalign="0" use-markup="true"><label>'$label'</label><variable>'$1'TitleShow</variable></text>
	<vbox space-fill="true" space-expand="false" spacing="2">
		<togglebutton space-fill="false" space-expand="false" theme-icon-size="8" relief="2">
			<variable>b'$RANDOM'</variable>
			<input file icon="go-first"></input>
			<action>if true hide:'$1'</action>
			<action>if false show:'$1'</action>
			<action>if true hide:'$1'TitleShow</action>
			<action>if false show:'$1'TitleShow</action>
			<action>if true show:'$1'TitleHide</action>
			<action>if false hide:'$1'TitleHide</action>
		</togglebutton>
		<text wrap="false" angle="90" visible="false"><label>Show '$label'</label><variable>'$1'TitleHide</variable></text>
	</vbox>
	<variable>'$1'Toggle</variable>
	</hbox>'
};export -f gContainerHideShow

function gContainerHorizontal()
{
local label=$1
if [ -n "$2" ]
then
	label=$2
fi
echo '<hbox space-fill="true" space-expand="false" spacing="0">
	<text space-expand="true" xalign="0" use-markup="true"><label>'$label'</label><variable>'$1'TitleShow</variable></text>
	<vbox space-fill="true" space-expand="false" spacing="2">
		<togglebutton space-fill="false" space-expand="false" theme-icon-size="8" relief="2">
			<variable>b'$RANDOM'</variable>
			<input file icon="go-first"></input>
			<action>if true hide:'$1'</action>
			<action>if false show:'$1'</action>
			<action>if true hide:'$1'TitleShow</action>
			<action>if false show:'$1'TitleShow</action>
			<action>if true show:'$1'TitleHide</action>
			<action>if false hide:'$1'TitleHide</action>
		</togglebutton>
		<text wrap="false" visible="false"><label>Show '$label'</label><variable>'$1'TitleHide</variable></text>
	</vbox>
	<variable>'$1'Toggle</variable>
	</hbox>'
};export -f gContainerHorizontal
function gPlaceholder()
{
echo '<button space-fill="true" space-expand="true" spacing="0">
		<label>[ '$1' ]</label>
		<variable>b'$RANDOM'</variable>
		<input file icon="emblem-favorite"></input>
		<action signal="clicked">echo This is '$1'</action>
	</button>
	<button space-fill="true" space-expand="true" spacing="0" theme-icon-size="32" image-position="2">
		<label>[ Widget ]</label>
		<variable>b'$RANDOM'</variable>
		<input file icon="applications-engineering"></input>
		<action signal="clicked">echo This is '$1' component</action>
	</button>
	<button space-fill="true" space-expand="true" spacing="0" theme-icon-size="32" image-position="2">
		<label>[ Widget ]</label>
		<variable>b'$RANDOM'</variable>
		<input file icon="applications-other"></input>
		<action signal="clicked">echo This is '$1' component</action>
	</button>
	<button space-fill="true" space-expand="true" spacing="0" theme-icon-size="32" image-position="2">
		<label>About</label>
		<variable>b'$RANDOM'</variable>
		<input file icon="applications-science"></input>
		<action signal="clicked">XMLRunDialog About</action>
	</button>'
};export -f gPlaceholder

function gVBox()
{
echo '<vbox space-fill="true" space-expand="true" spacing="0">'
gPlaceholder Main
echo '</vbox>'
};export -f gVBox
function gHBox()
{
echo '<hbox space-fill="true" space-expand="true" spacing="0">'
gPlaceholder Main
echo '</hbox>'
};export -f gHBox
function gExitButton()
{
echo '<hbox space-fill="true" space-expand="false" spacing="0">
		<button space-fill="true" space-expand="true" spacing="0">
			<label>Exit</label>
			<variable>b'$RANDOM'</variable>
			<input file stock="gtk-quit"></input>
		</button>
	</hbox>'
};export -f gExitButton	


function gmEntry()
{
	#	'"$(gFormEntry varname icon text default)"'
echo '<hbox>
	<entry primary-icon-stock="'$2'"  secondary-icon-name="'$GMICON_ENTRY_CLEAR'" 
	primary-icon-activatable="false" secondary-icon-activatable="true"
	 width-request="150">
		<sensitive>false</sensitive>
		<action signal="primary-icon-press">clear:v'$1'</action>
		<action signal="secondary-icon-press">clear:v'$1'</action>
		<default>'$4'</default>
		<variable>v'$1'</variable>
	</entry>
</hbox>'
};export -f gmEntry

function TreeNodeInsert()
{
	local source=$1
	local newdata=$2
	echo "${!newdata}" >> "$source"
};export -f TreeNodeInsert
function TreeNodeMoveUp
{
	local source=$1
	local tree=$2
	local newrow=${!tree}
	let "newrow-=1"
	if [ "$newrow" -gt "0" ]
	then
		local aboverow=`sed -n "${newrow}p" $source`
		local currentrow=`sed -n "${!tree}p" $source`
		sed  --in-place -e "${newrow}s%$aboverow%$currentrow%" $source
		sed  --in-place -e "${!tree}s%$currentrow%$aboverow%" $source
	fi
};export -f TreeNodeMoveUp
function TreeNodeMoveDown
{
	local source=$1
	local tree=$2
	local newrow=${!tree}
	let "newrow+=1"
	local rowbelow=`sed -n "${newrow}p" $source`
	local currentrow=`sed -n "${!tree}p" $source`
	sed  --in-place -e "${!tree}s%$currentrow%$rowbelow%" $source
	sed  --in-place -e "${newrow}s%$rowbelow%$currentrow%" $source
};export -f TreeNodeMoveDown
function TreeNodeInsertBefore
{
	local source=$1
	local tree=$2
	local newdata=$3
	local index=1
	echo -n "" > $PROGTMP/${tree}.buffer
	cat $source |
	while read line  # For as many lines as the input file has...
		do
			if [ "$index" -eq "${!tree}" ]
			then
				echo "${!newdata}"   >>  $PROGTMP/${tree}.buffer
			fi
		echo "$line"   >>  $PROGTMP/${tree}.buffer
		let "index+=1"
	done
	cp --force --update $PROGTMP/${tree}.buffer $source
};export -f TreeNodeInsertBefore
function TreeNodeInsertAfter
{
	local source=$1
	local tree=$2
	local newdata=$3
	local index=0
	echo -n "" > $PROGTMP/${tree}.buffer
	cat $source |
	while read line  # For as many lines as the input file has...
		do
			if [ "$index" -eq "${!tree}" ]
			then
				echo "${!newdata}"   >>  $PROGTMP/${tree}.buffer
			fi
		echo "$line"   >>  $PROGTMP/${tree}.buffer
		let "index+=1"
	done
	cp --force --update $PROGTMP/${tree}.buffer $source
};export -f TreeNodeInsertAfter
function TreeNodeDelete()
{
	local source=$1
	local tree=$2
	sed --in-place -e "${!tree}d" "$source"
	
	echo "" > $PROGTMP/${tree}.data
	echo -n "<b>File:</b><span color=\"DarkGreen\">${source}</span>
<span color=\"DarkRed\">Select a node</span>" >  $PROGTMP/${tree}.cursor
};export -f TreeNodeDelete
function TreeNodeDeleteBefore()
{
	local source=$1
	local tree=$2
	local before=${!tree}
	let "before-=1"
	sed --in-place -e "${before}d" "$source"
	
	echo "" > $PROGTMP/${tree}.data
	echo -n "<b>File:</b><span color=\"DarkGreen\">${source}</span>
<span color=\"DarkRed\">Select a node</span>" >  $PROGTMP/${tree}.cursor
};export -f TreeNodeDeleteBefore
function TreeNodeDeleteAfter()
{
	local source=$1
	local tree=$2
	local after=${!tree}
	let "after+=1"
	sed --in-place -e "${after}d" "$source"
	
	echo "" > $PROGTMP/${tree}.data
	echo -n "<b>File:</b><span color=\"DarkGreen\">${source}</span>
<span color=\"DarkRed\">Select a node</span>" >  $PROGTMP/${tree}.cursor
};export -f TreeNodeDeleteAfter

function TreeNodeCopy()
{
local source=$1
local tree=$2
cat $PROGTMP/${tree}.data >> "${source}"
};export -f TreeNodeCopy
function DCPipetoList()
{
local ifssaved=$IFS; IFS="|";
DCExplode "" "" $*
IFS=$ifsaved
};export -f DCPipetoList
function TreeNodeForm()
{
local tree=$1
local header=${tree}Headers
local index=1 

DCPipetoList ${!header} |
while read line  # For as many lines as the input file has...
	do
	if [ "$index" -gt "0" ]
	then
		echo '
			<hbox spacing="0">
				<text space-fill="false" space-expand="false">
					<label>'$line' : </label>
				</text>
				<entry>
					<variable>'${tree}'Input'$index'</variable>
					<default>'${!header}' -- </default>
				</entry>
			</hbox>'
	fi
	let "index+=1"
	done
};export -f TreeNodeForm
function TreeNodeEdit()
{
local source=$1
local tree=$2
local newdata=$3
local olddata=`cat $PROGTMP/$tree.data`
sed --in-place -e "${!tree}s%$olddata%${!newdata}%"  "${source}"
};export -f TreeNodeEdit
function TreeNodeRead()
{
local index=1 
cat $1 |
while read line  # For as many lines as the input file has...
	do
	echo "$index|$line"   # Output the line itself.
	let "index+=1"
	done
};export -f TreeNodeRead

function TreeNodeCursorChanged()
{
	local source=$1
	local tree=$2
	local data=`sed -n ${!tree}p "$1"`
	local exportcolumn=${tree}ExportColumn
	TreeNodeExportColumn ${!exportcolumn} $data > $PROGTMP/${tree}
	echo $data > $PROGTMP/${tree}.data
	echo -n "<b>File:</b><span color=\"DarkGreen\">${source}</span>
<b>Cursor at line:</b><span color=\"Blue\">${!tree}</span>" >  $PROGTMP/${tree}.cursor
};export -f TreeNodeCursorChanged

function TreeNodeGetColumn()
{
	local col=$1
	let "col+=2"
	# 1 is row, 2 is icon, thats why +2
	# data file should have icon|data1|data2|etc, no need row number
	echo  "${!col}"
};export -f TreeNodeGetColumn
function TreeNodeExportColumn()
{
	local col=$1
	shift
	local data=$*
	local OIFS=$IFS
	export IFS="|"
	TreeNodeGetColumn $col $data
	export IFS=$OIFS
};export -f TreeNodeExportColumn
function InjectXML()
{
echo $*
};export -f InjectXML
function InjectWidgets()
{
echo $*
};export -f InjectWidgets

function gmTreeView()
{
local tree=$1
local legend=${tree}Legend
local headers=${tree}Headers
local source=${tree}
local icon=${tree}Icon
local colvisible=${tree}ColVisible
local customdirective=${tree}Directives
local customcomponent=${tree}Component
local iconcolumn=${tree}IconColumn
local editable=${tree}Editable
local appendable=${tree}Appendable

local oninsertbefore=${tree}OnInsertBefore
local oninsert=${tree}OnInsert
local oninsertafter=${tree}OnInsertAfter
local oncopy=${tree}OnCopy
local onedit=${tree}OnEdit
local ondeletebefore=${tree}OnDeleteBefore
local ondelete=${tree}OnDelete
local ondeleteafter=${tree}OnDeleteAfter
#if [ ! -e "${!tree}.data" ]
#then
#	echo -n "" > ${!tree}.data
#fi
#if [ ! -e "${!tree}.cursor" ]
#then
#	echo -n "" > ${!tree}.cursor
#fi
echo "" > $PROGTMP/${tree}.data
echo -n "<b>File:</b><span color=\"DarkGreen\">${source}</span>
<span color=\"DarkRed\">Select a node</span>" >  $PROGTMP/${tree}.cursor
if [ -z "${tree}ExportColumn" ]
then
	${tree}ExportColumn="0"
fi
echo '
<vbox space-fill="true" space-expand="true" spacing="0">
<frame '${!legend}'>
<hbox space-fill="true" space-expand="true" spacing="0">
	<tree  space-fill="true" space-expand="true" rules-hint="true" enable-tree-lines="true" exported-column="0" icon-name="'${!icon}'" selected-row="0" column-visible="true|'${!colvisible}'" auto-refresh="true" file-monitor="true">
		<variable>'${tree}'</variable>
		<label>#|'${!headers}'</label>
		<input'${!iconcolumn}'>TreeNodeRead '${!source}'</input>
		<output file>'${!source}'</output>
		<action signal="cursor-changed">TreeNodeCursorChanged  "'${!source}'" "'${tree}'"</action>'
		InjectXML ${!customdirective}
echo '	<action signal="cursor-changed">refresh:'${tree}'Input</action>
		<action signal="cursor-changed">refresh:'${tree}'Cursor</action>
	</tree>
</hbox>
	<vbox space-fill="false" space-expand="false" spacing="0">
		<vbox spacing="0">
			<hbox space-fill="true" space-expand="true" spacing="0">
				<vbox spacing="0">
					<hbox spacing="0" space-fill="true" space-expand="true">
						<text space-fill="true" space-expand="true" xalign="0" use-markup="true">
							<variable>'${tree}'Cursor</variable>
							<input file>'$PROGTMP'/'${tree}'.cursor</input>
						</text>
					</hbox>
					<entry tooltip-text="Quick edit">
						<variable>'${tree}'Input</variable>
						<input file>'$PROGTMP'/'${tree}'.data</input>
					</entry>
				</vbox>
			</hbox>
		</vbox>
	<hbox space-fill="true" space-expand="true" spacing="0">
		<hseparator space-fill="false" space-expand="false"></hseparator>'
if [ "${!editable}" == "true" ]
then
echo '	<vbox space-fill="true" space-expand="true"  spacing="0">
			<button space-fill="true" space-expand="true" tooltip-text="Edit">
				<input file icon="'$GMICON_NODE_EDIT'"></input>
				<label>Edit</label>'
InjectXML ${!onedit}
echo '			<action>TreeNodeEdit "'${!source}'" "'${tree}'" "'${tree}'Input"</action>
				<action>refresh:'${tree}'</action>
				<action>refresh:'${tree}'Cursor</action>
			</button>
			<button space-fill="true" space-expand="true" tooltip-text="Delete">
				<input file icon="'$GMICON_NODE_DELETE'"></input>
				<label>Delete</label>'
InjectXML ${!ondelete}
echo '			<action>TreeNodeDelete "'${!source}'"  "'${tree}'" </action>
				<action>removeselected:"'${tree}'"</action>
				<action>refresh:'${tree}'</action>
				<action>refresh:'${tree}'Cursor</action>
			</button>
		</vbox>
		<vbox space-fill="false" space-expand="false"  spacing="0">
			<button space-fill="false" space-expand="false" theme-icon-size="16" tooltip-text="Delete before">'
InjectXML ${!ondeletebefore}
echo '			<input file icon="'$GMICON_NODE_DELETE_BEFORE'"></input>
				<action>TreeNodeDeleteBefore "'${!source}'" "'${tree}'"</action>
				<action>refresh:'${tree}'</action>
				<action>refresh:'${tree}'Cursor</action>
			</button>
			<button space-fill="false" space-expand="false" theme-icon-size="16" tooltip-text="Delete after">
				<input file icon="'$GMICON_NODE_DELETE_AFTER'"></input>'
InjectXML ${!ondeleteafter}
echo '			<action>TreeNodeDeleteAfter "'${!source}'" "'${tree}'"</action>
				<action>refresh:'${tree}'</action>
				<action>refresh:'${tree}'Cursor</action>
			</button>
		</vbox>'
fi
if [ "${!appendable}" == "true" ]
then
echo '
		<vbox space-fill="true" space-expand="true" spacing="0">
			<button space-fill="true" space-expand="true" tooltip-text="Copy">
				<input file icon="'$GMICON_NODE_COPY'"></input>
				<label>Copy</label>'
InjectXML ${!oncopy}
echo '			<action>TreeNodeCopy "'${!source}'"  "'${tree}'"</action>
				<action>refresh:'${tree}'</action>
				<action>refresh:'${tree}'Cursor</action>
			</button>
			<button space-fill="true" space-expand="true" tooltip-text="Insert">
				<input file icon="'$GMICON_NODE_INSERT'"></input>
				<label>Insert</label>'
InjectXML ${!oninsert}
echo '			<action>TreeNodeInsert "'${!source}'" "'${tree}'Input"</action>
				<action>refresh:'${tree}'</action>
				<action>refresh:'${tree}'Cursor</action>
			</button>
		</vbox>
		<vbox space-fill="false" space-expand="false" spacing="0">
			<button space-fill="false" space-expand="false" theme-icon-size="16" tooltip-text="Insert before">
				<input file icon="'$GMICON_NODE_INSERT_BEFORE'"></input>'
InjectXML ${!oninsertbefore}
echo '			<action>TreeNodeInsertBefore "'${!source}'" "'${tree}'" "'${tree}'Input"</action>
				<action>refresh:'${tree}'</action>
				<action>refresh:'${tree}'Cursor</action>
			</button>
			<button space-fill="false" space-expand="false" theme-icon-size="16" tooltip-text="Insert after">
				<input file icon="'$GMICON_NODE_INSERT_AFTER'"></input>'
InjectXML ${!oninsertafter}
echo '			
				<action>TreeNodeInsertAfter "'${!source}'" "'${tree}'" "'${tree}'Input"</action>
				<action>refresh:'${tree}'</action>
				<action>refresh:'${tree}'Cursor</action>
			</button>
		</vbox>'
fi
echo '
		<button space-fill="true" space-expand="true" tooltip-text="Reload the treeview" image-position="2">
			<label>Reload</label>
			<input file icon="'$GMICON_NODE_REFRESH'"></input>
			<action>refresh:'${tree}'</action>
			<action>refresh:'${tree}'Cursor</action>
		</button>
		<vbox space-fill="false" space-expand="false" spacing="0">
			<button space-fill="false" space-expand="false" theme-icon-size="16" tooltip-text="Move up">
				<input file icon="'$GMICON_NODE_MOVE_UP'"></input>
				<action>TreeNodeMoveUp "'${!source}'"  "'${tree}'" </action>
				<action>refresh:'${tree}'</action>
				<action>refresh:'${tree}'Cursor</action>
			</button>
			<button space-fill="false" space-expand="false" theme-icon-size="16" tooltip-text="Move down">
				<input file icon="'$GMICON_NODE_MOVE_DOWN'"></input>
				<action>TreeNodeMoveDown "'${!source}'"  "'${tree}'" </action>
				<action>refresh:'${tree}'</action>
				<action>refresh:'${tree}'Cursor</action>
			</button>
		</vbox>
	</hbox>'
InjectXML ${!customcomponent}
echo '
	</vbox>
</frame>
</vbox>'

};export -f gmTreeView
function gmAddWidget()
{
	local source=$1
	if [ -e "$PROGDIR/xml/$1.gxml" ]
	then
		cat "$PROGDIR/xml/$1.gxml"
	elif [ -e "$PROGDIR/xml/$1.xml" ]
	then
		cat "$PROGDIR/xml/$1.xml"
	else
		echo "<hbox><text><label>Widget named '$1' not found</label></text></hbox>"
	fi
};export -f gmAddWidget
# export -f
